package com.example.springboot.controller;

import com.example.springboot.entity.Message;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class MessageController {
    private List<Message> messages = new ArrayList<>();
    private AtomicInteger idCounter = new AtomicInteger(1);

    @PostMapping("/sendMessage")
    @CrossOrigin
    public String sendMessage(@RequestBody Message message) {
        message.setId(idCounter.getAndIncrement());
        messages.add(message);
        return "消息发送成功";
    }

    @GetMapping("/getMessages")
    @CrossOrigin
    public List<Message> getMessages(@RequestParam String recipient) {
        List<Message> userMessages = new ArrayList<>();
        for (Message msg : messages) {
            if (msg.getRecipient().equals(recipient) || msg.getRecipient().equals("system")) {
                userMessages.add(msg);
            }
        }
        return userMessages;
    }
}
